# --*--coding=utf-8
"""
日期模块常用的方法
"""
import datetime

DATE_FMT = '%Y-%m-%d'
cur_day = datetime.date.today()


def add_days(date=cur_day, days=0, date_fmt=DATE_FMT):
    """
    日期加减算法
    :param date:
    :param days:
    :param date_fmt:
    :return:
    """
    return (date + datetime.timedelta(days=days)).strftime(date_fmt)


def date_to_str(date, date_fmt=DATE_FMT):
    """
    :param date:
    :param date_fmt:
    :return:
    """
    return date.strftime(date_fmt)


def str_to_date(s):
    """
    获取当前日期
    :param s:
    :return:
    """
    return datetime.strptime(s, DATE_FMT)


def add(date, days):
    """
    :param date:日期天数
    :param days: 加之后返回的日期对象
    :return:
    """
    return (str_to_date(date) + datetime.timedelta(days=days)).strftime(DATE_FMT)


def differ(date1, date2):
    """
    :param date1: 日期字符串
    :param date2: 日期字符串
    :return: 相差天数
    """
    d1 = str_to_date(date1)
    d2 = str_to_date(date2)
    if d1 < d2:
        d1, d2 = d2, d1
    return (d1 - d2).days
